package ChatNI;

import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.net.Socket;

import Controller.User;
import Signals.AbstractMessage;
import Signals.FileMessage;


public class TCPReceivingFileTransfert extends Thread {

	protected volatile boolean running = true;
	private Socket sock;
	private TCPServer tcpsrv;
	private int ID;

	/**
	 * Initialize the attributes 
	 * @param sock Network du Chat System
	 * @param tcpsrv TCP Server of the Chat System
	 * @ID id of the TCPReceivingFileTransfert
	 */
	public TCPReceivingFileTransfert(Socket sock, TCPServer tcpsrv, int ID) {
		this.sock = sock;
		this.ID = ID;
		this.tcpsrv = tcpsrv;
	}

	public void run() {
		while(running) {
			try {
				InputStream is = sock.getInputStream();

				System.out.println("TCPReceivingFileTransfert: run: Socket created");

				byte[] data = new byte[1024];

				is.read(data);
				ByteArrayInputStream bais = new ByteArrayInputStream(data);
				ObjectInputStream ois = new ObjectInputStream(bais);
				//we collect the object present in the packet
				AbstractMessage receivedFile;
				try {
					receivedFile = (AbstractMessage) ois.readObject();

					//Casting the receiving Abstract message into a FileMessage
					FileMessage filemsg = (FileMessage) receivedFile;
					System.out.println("TCPReceivingFileTransfert:run:the file name is:" + filemsg.getNamefile());

					//Writing the file on the disk


					System.out.println("TCPReceivingFileTransfert:run:the file exist");
					System.out.println("TCPReceivingFileTransfert:run:receiving the file");
					System.out.println("TCPReceivingFileTransfert:run:file received");
					FileOutputStream fos = new FileOutputStream("./" + filemsg.getNamefile());

					System.out.println("TCPReceivingFileTransfert:run:coping the file");
					//Récupère l'Inputstream du socket
                                        byte[] buffer = new byte[1000];
                                        int bytesRead;
                                        InputStream inputstr = this.sock.getInputStream();
                                        long count=0;
                                        do {
                                                bytesRead= inputstr.read(buffer);
                                                count+= bytesRead;                                      
                                                fos.write(buffer, 0, bytesRead);
                                        }while (count<filemsg.getFileSize());


					ois.close();
					fos.close();
					inputstr.close();
					inputstr.close();
					System.out.println("TCPReceivingFileTransfert:run:closing everything");

					User remoteUser = new User(filemsg.getNickname());
					tcpsrv.getNI().processFileMessage(remoteUser, filemsg.getNamefile(), User.fromStringListToUserList(filemsg.getDest()));
					sock.close();

					tcpsrv.closeConnection(ID);

				} catch (ClassNotFoundException ex) {
					System.out.println("Class not found ");
				}

			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		this.interrupt();
	}

	/**
	 * Aimed to set the boolean running
	 * @param running boolean 
	 */
	public void setRunning(boolean running) {
		this.running = running;
	}


}
